/**
 * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.sys.utils;

import cn.apestar.common.DataContants;
import cn.apestar.common.DataContants.AuditType;
import cn.apestar.common.enums.EnumsList;
import cn.apestar.common.utils.BeanUtil;
import cn.apestar.common.utils.CacheUtils;
import cn.apestar.common.utils.SpringContextHolder;
import cn.apestar.core.base.BaseDictEntity;
import cn.apestar.modules.common.school.entity.School;
import cn.apestar.modules.common.school.service.SchoolService;
import cn.apestar.modules.common.version.dao.AppVersionDao;
import cn.apestar.modules.common.version.entity.AppVersion;
import cn.apestar.modules.sys.dao.DictDao;
import cn.apestar.modules.sys.entity.Area;
import cn.apestar.modules.sys.entity.Dict;
import cn.apestar.modules.sys.entity.Institution;
import cn.apestar.modules.sys.enums.RoleTypeEnum;
import cn.apestar.modules.sys.service.InstitutionService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典工具类
 *
 * @author ThinkGem
 * @version 2013-5-29
 */
public class DictUtils {

    private static DictDao dictDao = SpringContextHolder.getBean(DictDao.class);

    private static AppVersionDao appVersionDao = SpringContextHolder.getBean(AppVersionDao.class);

    public static final String CACHE_DICT_MAP = "dictMap";

    public static final String CACHE_APP_MAP = "APP_VERSION";

    public static String getDictLabel(String value, String type, String defaultValue) {

        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)) {
            for (Dict dict : getDictList(type)) {
                if (type.equals(dict.getType()) && value.equals(dict.getCode())) {
                    return dict.getLabel();
                }
            }
        }
        return defaultValue;
    }

    public static String getVersionLabel(String value, String defaultValue) {

        if (StringUtils.isNotBlank(value)) {
            for (Dict dict : getVersionList()) {
                if (value.equals(dict.getCode())) {
                    return dict.getLabel();
                }
            }
        }
        return defaultValue;
    }

    public static String getDictLabels(String values, String type, String defaultValue) {

        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)) {
            List<String> valueList = Lists.newArrayList();
            for (String value : StringUtils.split(values, ",")) {
                valueList.add(getDictLabel(value, type, defaultValue));
            }
            return StringUtils.join(valueList, ",");
        }
        return defaultValue;
    }

    public static String getDictValue(String label, String type, String defaultLabel) {

        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)) {
            for (Dict dict : getDictList(type)) {
                if (type.equals(dict.getType()) && label.equals(dict.getLabel())) {
                    return dict.getCode();
                }
            }
        }
        return defaultLabel;
    }

    public static List<Dict> getVersionList() {
        Map<String, List<Dict>> dictMap = (Map<String, List<Dict>>) CacheUtils.get(CACHE_APP_MAP);
        if (dictMap == null) {
            dictMap = Maps.newHashMap();
            List<AppVersion> findAllList = appVersionDao.findAllList(new AppVersion());
            for (AppVersion dict : findAllList) {
                List<Dict> dictList = dictMap.get(CACHE_APP_MAP);
                Dict verDict = new Dict();
                verDict.setLabel(dict.getVersionName());
                verDict.setCode(dict.getId());
                verDict.setType(CACHE_APP_MAP);
                if (dictList != null) {
                    dictList.add(verDict);
                } else {
                    dictMap.put(CACHE_APP_MAP, Lists.newArrayList(verDict));
                }
            }
            CacheUtils.put(CACHE_APP_MAP, dictMap);
        }
        List<Dict> dictList = dictMap.get(CACHE_APP_MAP);
        if (dictList == null) {
            dictList = Lists.newArrayList();
        }
        return dictList;
    }

    public static List<Dict> getDictList(String type) {

        Map<String, List<Dict>> dictMap = (Map<String, List<Dict>>) CacheUtils.get(CACHE_DICT_MAP);
        dictMap = null;
        if (dictMap == null) {
            dictMap = Maps.newHashMap();
            List<Dict> findAllList = dictDao.findAllList(new Dict());
            for (Dict dict : findAllList) {
                List<Dict> dictList = dictMap.get(dict.getType());
                if (dictList != null) {
                    dictList.add(dict);
                } else {
                    dictMap.put(dict.getType(), Lists.newArrayList(dict));
                }
            }
            CacheUtils.put(CACHE_DICT_MAP, dictMap);
        }
        List<Dict> dictList = dictMap.get(type);
        if (dictList == null) {
            dictList = Lists.newArrayList();
        }
        return dictList;
    }

    public static List<Dict> getShangQuanList() {

        List list = new ArrayList();
        List shangQuanList = UserUtils.getShangQuanList();
        for (int i = 0; i < shangQuanList.size(); i++) {
            Area area = (Area) shangQuanList.get(i);
            Dict dict = new Dict();
            dict.setCode(area.getId());
            dict.setLabel(area.getName());
            dict.setId(area.getId());
            list.add(dict);
        }
        return list;
    }

    public static List<Dict> getAuditState() {

        List list = new ArrayList();
        // list.add(new Dict(AuditType.NOT_AUDIT, AuditType.NOT_AUDIT, "未审核"));
        list.add(new Dict(AuditType.AUDIT_WAIT, AuditType.AUDIT_WAIT, "等待审核"));
        list.add(new Dict(AuditType.AUDIT_PASS, AuditType.AUDIT_PASS, "审核通过"));
        list.add(new Dict(AuditType.AUDIT_NO_PASS, AuditType.AUDIT_NO_PASS, "审核不通过"));
        return list;
    }

    public static List<Dict> getPayState() {

        List list = new ArrayList();
        list.add(new Dict("", "全部"));
        list.add(new Dict("notpay", "未支付"));
        list.add(new Dict("success", "支付成功"));
        list.add(new Dict("closed", "关闭支付"));
        list.add(new Dict("fail", "支付失败"));
        return list;
    }

    public static List<Dict> getOrderType() {

        List list = new ArrayList();
        list.add(new Dict("", "全部"));
        list.add(new Dict("pay", "支付"));
        list.add(new Dict("withdraw", "撤销"));
        list.add(new Dict("refund", "退款"));
        return list;
    }

    public static String getValForList(List<BaseDictEntity> listName, String val) {
        for (BaseDictEntity entity : listName) {
            if (entity.getCode().equals(val)) {
                return entity.getLabel();
            }
        }
        return "无";
    }

    public static List<Dict> getTerimalType() {
        List list = new ArrayList();
        list.add(new Dict("", "全部"));
        list.add(new Dict("alipay", "支付宝"));
        list.add(new Dict("wechat", "微信"));
        return list;
    }

    public static List<Map> getDictListMap(String type) {

        Map<String, List<Map>> dictMap =
            (Map<String, List<Map>>) CacheUtils.get(CACHE_DICT_MAP + "MAP");
        if (dictMap == null) {
            dictMap = Maps.newHashMap();
            List<Dict> findAllList = dictDao.findAllList(new Dict());
            for (Dict dict : findAllList) {
                List<Map> dictList = dictMap.get(dict.getType());
                Map map = new HashMap();
                map.put("value", dict.getCode());
                map.put("remark", dict.getRemarks());
                map.put("description", dict.getDescription());
                map.put("label", dict.getLabel());
                if (dictList != null) {
                    dictList.add(map);
                } else {
                    dictMap.put(dict.getType(), Lists.newArrayList(map));
                }
            }
            CacheUtils.put(CACHE_DICT_MAP + "MAP", dictMap);
        }
        List<Map> dictList = dictMap.get(type);
        if (dictList == null) {
            dictList = Lists.newArrayList();
        }
        return dictList;
    }

    /**
     * 获取学区信息
     *
     * @return List<School>
     * @author lvh 2016年2月23日 上午9:20:41</br>
     */
    public static List<School> getSchoolList() {
        SchoolService schoolService = SpringContextHolder.getBean(SchoolService.class);
        return schoolService.findList(new School());
    }

    /**
     * 获取机构信息
     *
     * @return List<Institution>
     * @author lvh 2016年2月23日 上午9:20:53</br>
     */
    public static List<Institution> getInstitutionListByType(String institutionType) {

        InstitutionService institutionService =
            SpringContextHolder.getBean(InstitutionService.class);
        Institution institutionFilter = new Institution();
        institutionFilter.setInstitutionType(institutionType);
        List<Institution> list = institutionService.findList(institutionFilter);
        for (int i = 0; i < list.size(); i++) {
            Institution o = list.get(i);
            o.setInstitutionName(o.getId() + "|" + o.getInstitutionName());
        }
        Institution i = new Institution();
        i.setId("");
        i.setInstitutionName("===空===");
        list.add(0, i);
        return list;
    }

    /**
     * 获取机构信息
     *
     * @return List<Institution>
     * @author lvh 2016年2月23日 上午9:20:53</br>
     */
    public static List<Institution> getInstitutionList() {

        InstitutionService institutionService =
            SpringContextHolder.getBean(InstitutionService.class);
        Institution institutionFilter = new Institution();
        List<Institution> list = institutionService.findList(institutionFilter);
        for (int i = 0; i < list.size(); i++) {
            Institution o = list.get(i);
            o.setInstitutionName(o.getId() + "|" + o.getInstitutionName());
        }
        Institution i = new Institution();
        i.setId("");
        i.setInstitutionName("===空===");
        list.add(0, i);
        return list;
    }

    /**
     * 模特信息
     */
    public static List<Dict> getMtDataList(String mtType) {

        List<Dict> dictList = new ArrayList();
        List list = new ArrayList();

        // 模特国籍
        if ("MT_MTGJ".equals(mtType)) {
            list = UserUtils.getAreaByLevel(DataContants.AREA_GJ, null, null);
        }
        // 模特经验
        else if ("MT_MTCS".equals(mtType)) {
            list = UserUtils.getAreaByLevel(DataContants.AREA_SHI, null, null);
        }

        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);
            if (o instanceof Map) {
                Map map = (Map) list.get(i);
                Dict dictData = new Dict();
                dictData.setCode(ObjectUtils.toString(map.get("value"), ""));
                dictData.setLabel(ObjectUtils.toString(map.get("label"), ""));
                dictData.setRemarks(ObjectUtils.toString(map.get("remark"), ""));
                dictList.add(dictData);
            }
            if (o instanceof Area) {
                Area a = (Area) list.get(i);
                Dict dictData = new Dict();
                dictData.setCode(a.getId());
                dictData.setLabel(a.getName());
                dictData.setRemarks(a.getName());
                dictList.add(dictData);
            }
        }

        return dictList;
    }

    public static List<Dict> getRoleTypeList() {

        List<Dict> dictList = new ArrayList();
        Dict dictData = new Dict();
        dictData.setCode(RoleTypeEnum.PT.getValue());
        dictData.setLabel(RoleTypeEnum.PT.getName());
        dictList.add(dictData);

        Dict dictData1 = new Dict();
        dictData1.setCode(RoleTypeEnum.GLY.getValue());
        dictData1.setLabel(RoleTypeEnum.GLY.getName());
        dictList.add(dictData1);
        return dictList;
    }

    /**
     * 返回推荐指数
     *
     * @return List<Dict>
     * @author lvh 2016年4月6日 下午7:58:05</br>
     */
    public static List<Dict> getPaymentWithdrawList() {

        return EnumsList.getPaymentWithdrawList();
    }

    /**
     * 返回订单状态
     *
     * @return List<Dict>
     * @author lvh 2016年4月6日 下午7:58:05</br>
     */
    public static List<Dict> getOrderStatusList() {

        return EnumsList.getOrderStatusList();
    }

    /**
     * 返回推荐指数
     *
     * @return List<Dict>
     * @author lvh 2016年4月6日 下午7:58:05</br>
     */
    public static List<Dict> getOurRatings() {

        return EnumsList.getOurRatings();
    }

    /**
     * 返回推荐指数
     *
     * @param list
     * @return List<Dict>
     * @author lvh 2016年4月6日 下午7:58:05</br>
     */
    public static List translateList(List list, List<Dict> listDict, String formKey, String toKey) {

        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);
            String cd = ObjectUtils.toString(BeanUtil.getProperty(o, formKey), null);
            if (StringUtils.isEmpty(cd)) {
                return list;
            }
            StringBuffer buffer = new StringBuffer();
            for (Dict data : listDict) {
                String cd1 = data.getCode();
                if (cd.contains(",")) {
                    String[] cd1s = cd.split(",");
                    for (int u = 0; u < cd1s.length; u++) {
                        String cd2 = ObjectUtils.toString(cd1s[u], null);
                        if (cd2 != null && cd2.equals(cd1)) {
                            String ml = data.getLabel();
                            if (!StringUtils.isEmpty(ml)) {
                                buffer.append(ml);
                                buffer.append(",");
                            }
                        }
                    }
                    BeanUtil.setProperty(o, toKey, buffer.toString());
                } else {
                    if (cd.equals(cd1)) {
                        BeanUtil.setProperty(o, toKey, data.getLabel());
                        break;
                    }
                }
            }
        }
        return list;
    }
}
